package com.yaoxuguang.concurrence.enum_singleton_model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author yaoxuguang
 * @Title: yao-interview
 * @Package com.yaoxuguang.concurrence.enum_singleton_model
 * @Description: 用枚举实现单例模式
 * @date 2018年12月26日10:49
 */
public enum MyObject {
    connectionFactory;

    private Connection connection;
    private MyObject() {
        try {
            System.out.println("调用MyObject的构造方法");
            String url ="jdbc:mysql://localhost:3306/test";
            String username="root";
            String password="123456";
            String driverName="com.mysql.jdbc.Driver";
            Class.forName(driverName);
            connection = DriverManager.getConnection(url,username,password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public Connection getConnection(){
        return connection;
    }
}
